package com.ruoyi.coupon.listener;

import com.rabbitmq.client.Channel;
import com.ruoyi.common.core.flashpay.constant.CacheKey;
import com.ruoyi.common.core.flashpay.utils.IdWorker;
import com.ruoyi.coupon.domain.TbCoupon;
import com.ruoyi.coupon.domain.TbCouponRecord;
import com.ruoyi.coupon.service.ITbCouponRecordService;
import com.ruoyi.coupon.service.ITbCouponService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Component
public class CouponListener {

    @Autowired
    private ITbCouponService tbCouponService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ITbCouponRecordService iTbCouponRecordService;

    @Autowired
    private IdWorker idWorker;

    @RabbitListener(queues = {"delay-queue"})
    public void syncCouponCache(Long couponId, Channel channel, Message message) {
        //缓存入优惠券对应的数量
        try {
            TbCoupon tbCoupon = tbCouponService.selectTbCouponByCouponId(couponId);
            stringRedisTemplate.opsForHash().put(CacheKey.getCouponKey(couponId),"num",String.valueOf(tbCoupon.getNum()));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException ex) {
                throw new RuntimeException("拒绝消息队列编号:"+message.getMessageProperties().getDeliveryTag()+"   错误!");
            }
            throw new RuntimeException("缓存入优惠券对应的数量发生异常!");
        }
    }

    /**+
     * 监听领取优惠券队列  实现 优惠券领取记录业务
     * @param tbCouponRecord
     * @param channel
     * @param message
     */
    @RabbitListener(queues = {"coupon"})
    public void createCouponRecord(TbCouponRecord tbCouponRecord, Channel channel, Message message) {
        try {
            //mysql 优惠券总数量-1   领取数量+1
            tbCouponService.reduceNumByCouponId(tbCouponRecord.getCouponId());
            //增加优惠券领取记录表数据
            tbCouponRecord.setCouponRecordId(idWorker.nextId());
            tbCouponRecord.setStatus(0L);
            tbCouponRecord.setCreateTime(new Date());
           // iTbCouponRecordService.insertTbCouponRecord(tbCouponRecord);
            //删除redis中的优惠券缓存记录
            //stringRedisTemplate.opsForHash().delete(CacheKey.getCouponRecordKey(tbCouponRecord.getSikgaekId()), "coupon_" + tbCouponRecord.getCouponId());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException ex) {
                throw new RuntimeException("拒绝消息队列编号:"+message.getMessageProperties().getDeliveryTag()+"   错误!");
            }
            throw new RuntimeException("优惠券领取业务发生异常!");
        }
    }


}
